<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <script src="../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
			<span class="layui-breadcrumb">
            <a href="javascript:;">首页</a>
            <a>
              <cite>任务</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div style="padding-top: 15px">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-tab layui-tab-brief" lay-filter="demo">
                <ul class="layui-tab-title">
                    <li class="layui-this">与我相关</li>
                </ul>
                <div class="layui-tab-content" style="padding-left: 0px">
                    <div class="layui-tab-item layui-show">
                        <div class="layui-card" >
                            <div class="layui-card-body ">
                                <table class="layui-hide" id="taskTable" lay-filter="test"></table>
                                <script type="text/html" id="barDemo">
                                    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="start">开始</a>
                                    <a class="layui-btn layui-btn-xs" lay-event="finish">完成</a>
                                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                                </script>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['laydate', 'form', 'laypage', 'layer', 'table', 'element', 'carousel'], function () {
        var laydate = layui.laydate;
        var form = layui.form;
        var laypage = layui.laypage;
        var layer = layui.layer;
        var table = layui.table;
        var element = layui.element;
        var carousel = layui.carousel

        //实例化一个table
        table.render({
            elem: '#taskTable'
            ,id:'taskTable'
            , height: 300
            ,url: 'http://localhost:8080/task/mytask' //数据接口
            , title: '任务表'
            , page: true //开启分页
            , toolbar: true //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,defaultToolbar:['filter', 'exports']
            , cols: [[ //表头
                {
                    field: 'id',
                    title: 'ID',
                    width: '10%',
                    hide: true
                },{
                    field: 'taskPriority',
                    title: '级别',
                    width: '10%',
                    sort: true
                }, {
                    field: 'taskProjectCode',
                    title: '项目代号',
                    width: '20%',
                    minWidth: 100,
                    sort: true
                }, {
                    field: 'taskName',
                    title: '任务名',
                    width: '20%',
                    sort: true
                }, {
                    field: 'taskAssginedTo',
                    title: '指派给',
                    width: '10%',
                    sort: true
                }, {
                    field: 'taskStartDate',
                    title: '开始',
                    width: '10%',
                    sort: true
                }, {
                    field: 'taskEndDate',
                    title: '完成',
                    width: '10%',
                    sort: true
                },{
                    field: 'taskStatus',
                    title: '状态',
                    width: '10%',
                    sort: true
                },{
                    fixed: 'right',
                    width: '20%',
                    minWidth: 100,
                    align: 'center',
                    toolbar: '#barDemo'
                }
            ]]
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'start') {
                // xadmin.open('查看选中的事项','./todo-content.html');
                if(data.taskStatus=="进行中"||data.taskStatus=="已完成"){
                    layer.msg('已经开始或完成的任务不能开始',{
                        icon: 5,
                        time: 2000
                    });
                }else{
                    var myDate=new Date();
                    //获取当前时间并格式化yyyy-MM-dd HH:mm:ss
                    var nowDate=myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+
                        myDate.getDate()+" "+myDate.getHours()+":"+myDate.getMinutes()+":"+myDate.getSeconds();
                    data.taskStartDate=nowDate;
                    // layer.msg(nowDate);
                    $.ajax({
                        url: 'http://localhost:8080/task/start',
                        data: data,
                        contentType: "application/x-www-form-urlencoded",
                        type: 'POST',
                        xhrFields: {
                            withCredentials: true
                        },
                        success: function(data) {
                            if(data.status == "success") {
                                layer.msg('设置成功', {
                                    icon: 1,
                                    time: 2000
                                }, function() {
                                    //添加用户成功后关闭弹窗并刷新
                                    table.reload('taskTable',{
                                        url:'http://localhost:8080/task/mytask'
                                    })
                                });
                            } else {
                                layer.msg(data.data.errMsg, {
                                    icon: 5,
                                    time:1000
                                });
                            }
                        }
                    });
                    return false;
                }
            }
            else if (layEvent === 'del') {
                // xadmin.open('修改选中的事项','./todo-edit.html');
                layer.confirm('删除后将不可恢复',{icon:3,title:'提示'}, function(index){
                    layer.close(index);
                    $.ajax({
                        url: 'http://localhost:8080/task/delete',
                        data: data,
                        contentType: "application/x-www-form-urlencoded",
                        type: 'POST',
                        xhrFields: {
                            withCredentials: true
                        },
                        success: function(data) {
                            if(data.status == "success") {
                                obj.del();
                                layer.msg('删除成功', {
                                    icon: 1,
                                    time: 2000
                                }, function() {
                                    //添加用户成功后关闭弹窗并刷新
                                    layer.close(index);
                                    table.reload('taskTable',{
                                        url:'http://localhost:8080/task/mytask'
                                    })
                                });
                            } else {
                                layer.msg(data.data.errMsg, {
                                    icon: 5,
                                    time:1000
                                });
                            }
                        }
                    });
                    return false;
                });
            }
            else if (layEvent === 'finish') {
                // xadmin.open('查看选中的事项','./todo-content.html');
                if(data.taskStatus!="进行中"){
                    layer.msg('只能完成进行中的任务',{
                        icon: 5,
                        time: 2000
                    });
                }else{
                    var myDate=new Date();
                    //获取当前时间并格式化yyyy-MM-dd HH:mm:ss
                    var nowDate=myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+
                        myDate.getDate()+" "+myDate.getHours()+":"+myDate.getMinutes()+":"+myDate.getSeconds();
                    data.taskEndDate=nowDate;
                    // layer.msg(nowDate);
                    $.ajax({
                        url: 'http://localhost:8080/task/finish',
                        data: data,
                        contentType: "application/x-www-form-urlencoded",
                        type: 'POST',
                        xhrFields: {
                            withCredentials: true
                        },
                        success: function(data) {
                            if(data.status == "success") {
                                layer.msg('设置成功', {
                                    icon: 1,
                                    time: 2000
                                }, function() {
                                    //添加用户成功后关闭弹窗并刷新
                                    table.reload('taskTable',{
                                        url:'http://localhost:8080/task/mytask'
                                    })
                                });
                            } else {
                                layer.msg(data.data.errMsg, {
                                    icon: 5,
                                    time:1000
                                });
                            }
                        }
                    });
                    return false;
                }
            }
        });

        // 监听全选
        form.on('checkbox(checkall)', function (data) {

            if (data.elem.checked) {
                $('tbody input').prop('checked', true);
            } else {
                $('tbody input').prop('checked', false);
            }
            form.render('checkbox');
        });

    });
</script>
</body>

</html>